package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.annotation.BackLoginToken;
import com.sidianzhong.sdz.annotation.UserLoginToken;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.service.commond.RedisService;
import com.sidianzhong.sdz.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "转账")
@Controller
public class UserChangeController {

    @Autowired
    UserChangeService userChangeService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @UserLoginToken
    @ApiOperation(value = "创建'转账'表中一条信息")
    @RequestMapping(value = "/userChange/new", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createUserChange(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "fee", required = false) Double fee,
            @RequestParam(value = "getAmount", required = false) Double getAmount,
            @RequestParam(value = "drawHash", required = false) String drawHash,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lastUpdateTime", required = false) String lastUpdateTime
    ) {
        UserChange item = new UserChange();
        Date date = new Date();
        if( userId != null ){
            item.setUserId(userId);
        }
        if( address != null ){
            item.setAddress(address);
        }
        if( amount != null ){
            item.setAmount(amount);
        }
        if( remark != null ){
            item.setRemark(remark);
        }
        if( fee != null ){
            item.setFee(fee);
        }
        if( getAmount != null ){
            item.setGetAmount(getAmount);
        }
        if( drawHash != null ){
            item.setDrawHash(drawHash);
        }
        if( status != null ){
            item.setStatus(status);
        }
        if( type != null ){
            item.setType(type);
        }
        if( coinId != null ){
            item.setCoinId(coinId);
        }
            item.setCreateTime(new Date());
            item.setLastUpdateTime(new Date());
        UserChange iteams = userChangeService.create(item);
        boolean result = iteams != null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "删除'转账'表中的某条记录")
    @RequestMapping(value = "/userChange/delete",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteUserChange(
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
        UserChange item = userChangeService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        int delete = userChangeService.delete(id);
        boolean result = delete!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "修改'转账'表中的某条记录")
    @RequestMapping(value = "/userChange/edit",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editUserChange(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "fee", required = false) Double fee,
            @RequestParam(value = "getAmount", required = false) Double getAmount,
            @RequestParam(value = "drawHash", required = false) String drawHash,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "coinId", required = false) Integer coinId
    ) {
        UserChange item = userChangeService.get(id);
        if (null == item) {
           return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        if( userId != null ){
            item.setUserId(userId);
        }
        if( address != null ){
            item.setAddress(address);
        }
        if( amount != null ){
            item.setAmount(amount);
        }
        if( remark != null ){
            item.setRemark(remark);
        }
        if( fee != null ){
            item.setFee(fee);
        }
        if( getAmount != null ){
            item.setGetAmount(getAmount);
        }
        if( drawHash != null ){
            item.setDrawHash(drawHash);
        }
        if( status != null ){
            item.setStatus(status);
        }
        if( type != null ){
            item.setType(type);
        }
        if( coinId != null ){
            item.setCoinId(coinId);
        }
            item.setLastUpdateTime(new Date());
        int update =userChangeService.update(item);
        boolean result = update!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "查询'转账'表中的某条记录")
    @RequestMapping(value = "/getUserChangeById",method = {RequestMethod.GET},produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUserChangeById(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
            UserChange item = userChangeService.get(id);
            if (null == item) {
                return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
            }
             return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "查询'转账'表中的多条记录返回对象")
    @RequestMapping(value = "/userChanges",method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUserChanges(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "fee", required = false) Double fee,
            @RequestParam(value = "getAmount", required = false) Double getAmount,
            @RequestParam(value = "drawHash", required = false) String drawHash,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lastUpdateTime", required = false) String lastUpdateTime
    ) {
            UserChange item = new UserChange();
            if( userId != null ){
                item.setUserId(userId);
            }
            if( address != null ){
                item.setAddress(address);
            }
            if( amount != null ){
                item.setAmount(amount);
            }
            if( remark != null ){
                item.setRemark(remark);
            }
            if( fee != null ){
                item.setFee(fee);
            }
            if( getAmount != null ){
                item.setGetAmount(getAmount);
            }
            if( drawHash != null ){
                item.setDrawHash(drawHash);
            }
            if( status != null ){
                item.setStatus(status);
            }
            if( type != null ){
                item.setType(type);
            }
            if( coinId != null ){
                item.setCoinId(coinId);
            }
            if( createTime != null ){
                item.setCreateTime(Tools.convertStringToDate(createTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if( lastUpdateTime != null ){
                item.setLastUpdateTime(Tools.convertStringToDate(lastUpdateTime, "yyyy-MM-dd HH:mm:ss"));
            }
       PageInfo<UserChange> list =  userChangeService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
       return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "查询'转账'表中的多条记录返回自定义JSONObject")
    @RequestMapping(value = "/userChangesObject",method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUserChangesObject(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "amount", required = false) Double amount,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "fee", required = false) Double fee,
            @RequestParam(value = "getAmount", required = false) Double getAmount,
            @RequestParam(value = "drawHash", required = false) String drawHash,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "coinId", required = false) Integer coinId,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lastUpdateTime", required = false) String lastUpdateTime
    ) {
           UserChange item = new UserChange();
           if( userId != null ){
                item.setUserId(userId);
            }
            if( address != null ){
                item.setAddress(address);
            }
            if( amount != null ){
                item.setAmount(amount);
            }
            if( remark != null ){
                item.setRemark(remark);
            }
            if( fee != null ){
                item.setFee(fee);
            }
            if( getAmount != null ){
                item.setGetAmount(getAmount);
            }
            if( drawHash != null ){
                item.setDrawHash(drawHash);
            }
            if( status != null ){
                item.setStatus(status);
            }
            if( type != null ){
                item.setType(type);
            }
            if( coinId != null ){
                item.setCoinId(coinId);
            }
            if( createTime != null ){
                item.setCreateTime(Tools.convertStringToDate(createTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if( lastUpdateTime != null ){
                item.setLastUpdateTime(Tools.convertStringToDate(lastUpdateTime, "yyyy-MM-dd HH:mm:ss"));
            }
           PageInfo<JSONObject> list  =  userChangeService.getListWithObject(pageNum, pageSize, sortItem, sortOrder,item);
           List<JSONObject> list1 = list.getList();
           List<JSONObject> collect = list1.stream().map(ls -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",ls.getInteger("id"));
            jsonObject.put("userId",ls.getInteger("userId"));
            jsonObject.put("address",ls.getString("address"));
            jsonObject.put("amount",ls.getDouble("amount"));
            jsonObject.put("remark",ls.getString("remark"));
            jsonObject.put("fee",ls.getDouble("fee"));
            jsonObject.put("getAmount",ls.getDouble("getAmount"));
            jsonObject.put("drawHash",ls.getString("drawHash"));
            jsonObject.put("status",ls.getInteger("status"));
            jsonObject.put("type",ls.getInteger("type"));
            jsonObject.put("coinId",ls.getInteger("coinId"));
            jsonObject.put("createTime",ls.getDate("createTime"));
            jsonObject.put("lastUpdateTime",ls.getDate("lastUpdateTime"));
            return jsonObject;
           }).collect(Collectors.toList());
           list.setList(collect);
           return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
        }
}
